package com.portal.cwegportal.service;

import com.appcenter.jz.app.CurrentUser;
import com.portal.cwegportal.customenum.FileBusinessType;
import com.portal.cwegportal.customenum.FileClass;
import com.portal.cwegportal.input.meeting.*;
import com.portal.cwegportal.model.Meeting;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.model.MeetingNotice;
import com.portal.cwegportal.output.meeting.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.portal.cwegportal.mapper.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  创建人:王增立
 *  备注: 会议服务
 *  时间: 2019/4/26 15:08
 *  参数: 
 */
@Service
public class MeetingService {
    @Autowired
    MeetingNoticeMapper meetingNoticeMapper;
    @Autowired
    MeetingMapper meetingMapper;

    @Autowired
    FileService fileService;

    /**
     *  创建人:王增立
     *  备注: 创建会议通知、会议
     *  时间: 2019/4/26 15:08
     *  参数: 
     */
    @Transactional
    public CreateMeetingOutput createMeeting(CreateMeetingInput input) throws Exception {
        CreateMeetingOutput output = new CreateMeetingOutput();
        //操作会议表
        Meeting meeting = new Meeting();
        //ObjectUnit.Copy(input.getItem(), meeting);
        meeting.setEndTime(input.getItem().getEndTime());
        meeting.setStartTime(input.getItem().getStartTime());
        meeting.setTitle(input.getItem().getTitle());
        meeting.setHostCode(input.getItem().getHostCode());
        meeting.setHostId(input.getItem().getHostId());
        meeting.setHostName(input.getItem().getHostName());
        meeting.setMeetingroom(input.getItem().getMeetingroom());
        meeting.setContent(input.getItem().getContent());
        meeting.setCreatorCode(CurrentUser.getUserCode());
        meeting.setCreatorId(CurrentUser.getUserId());
        meeting.setCreatorName(CurrentUser.getUserName());
        meeting.setOriginatorCode(input.getItem().getOriginatorCode());
        meeting.setOriginatorId(input.getItem().getOriginatorId());
        meeting.setOriginatorName(input.getItem().getOriginatorName());
        meeting.setGmtCreate(new Date());
        meeting.setDeleted(false);
        meeting.setGmtStatus(input.getItem().getGmtStatus());

        meeting.setCreatorCode(CurrentUser.getUserCode());
        meeting.setCreatorId(CurrentUser.getUserId());
        meeting.setCreatorName(CurrentUser.getUserName());
        //插入会议表
        if(input.getItem().getId()!=null){
            meetingMapper.deleteByPrimaryKey(input.getItem().getId());// (input.getItem().getId());
        }
        meetingMapper.insertSelective(meeting);
        fileService.saveFiles(input.getItem().getFiles(), meeting.getId().toString(), FileBusinessType.Meeting.toString(), FileClass.File.toString());
        meeting.setFiles(fileService.getFiles(meeting.getId().toString(),FileBusinessType.Meeting.toString(),FileClass.File.toString()));
        Long id = meeting.getId();
        //操作通知表
        List<MeetingNotice> meetingNoticeList = new ArrayList<MeetingNotice>();

        input.getMeetingNoticeUserList().forEach((k) -> {
            MeetingNotice mn = new MeetingNotice();
            mn.setGmtCreate(new Date());
            mn.setGmtModified(meeting.getGmtModified());
            mn.setUserCode(k.getUserCode());
            mn.setUserId(k.getUserId());
            mn.setMeetingId(meeting.getId().toString());
            mn.setUserName(k.getUserName());
            mn.setCreatorCode(meeting.getCreatorCode());
            mn.setCreatorId(meeting.getCreatorId());
            mn.setCreatorName(meeting.getCreatorName());
            mn.setDeleted(meeting.isDeleted());
            meetingNoticeList.add(mn);
        });

        //插入通知表
        meetingNoticeList.forEach((k) -> {
            meetingNoticeMapper.insertSelective(k);
        });
        output.setItem(meeting);
        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 保存后进行编辑
     *  时间: 2019/4/26 15:08
     *  参数: 
     */
    @Transactional
    public EditMeetingOutput editMeeting(EditMeetingInput input) throws Exception {
        EditMeetingOutput output = new EditMeetingOutput();
        //查询会议主表并更新字段；
        Meeting oldEntity = meetingMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setContent(input.getItem().getContent());
        oldEntity.setDeleted(false);
        oldEntity.setEndTime(input.getItem().getEndTime());
        oldEntity.setStartTime(input.getItem().getStartTime());
        oldEntity.setGmtModified(new Date());
        oldEntity.setGmtStatus(input.getItem().getGmtStatus());
        oldEntity.setHostCode(input.getItem().getHostCode());
        oldEntity.setHostId(input.getItem().getHostId());
        oldEntity.setHostName(input.getItem().getHostName());
        oldEntity.setMeetingroom(input.getItem().getMeetingroom());
        oldEntity.setTitle(input.getItem().getTitle());
        oldEntity.setSortNo(input.getItem().getSortNo());
        meetingMapper.updateByPrimaryKey(oldEntity);
        //查询旧的通知并作废；
        Example epMeetingNotice = new Example(MeetingNotice.class);
        Example.Criteria ecMeetingNotice = epMeetingNotice.createCriteria();
        ecMeetingNotice.andEqualTo("meetingId", oldEntity.getId());
        List<MeetingNotice> meetingNoticeLs = meetingNoticeMapper.selectByExample(epMeetingNotice);
        meetingNoticeLs.forEach((k) -> {
            k.setDeleted(true);
            meetingNoticeMapper.updateByPrimaryKey(k);
        });
        //创建新的通知；
        List<MeetingNotice> meetingNoticeListNew = new ArrayList<MeetingNotice>();
        input.getMeetingNoticeUserList().forEach((k) -> {
            MeetingNotice mn = new MeetingNotice();
            mn.setGmtCreate(new Date());
            mn.setGmtModified(oldEntity.getGmtModified());
            mn.setUserCode(k.getUserCode());
            mn.setUserId(k.getUserId());
            mn.setMeetingId(input.getItem().getId().toString());
            mn.setUserName(k.getUserName());
            mn.setCreatorCode(oldEntity.getCreatorCode());
            mn.setCreatorId(oldEntity.getCreatorId());
            mn.setCreatorName(oldEntity.getCreatorName());
            mn.setDeleted(oldEntity.isDeleted());
            mn.setConfirmReplyTime(new Date());
            meetingNoticeListNew.add(mn);
        });
        meetingNoticeListNew.forEach((k) -> {
            meetingNoticeMapper.insertSelective(k);
        });
        fileService.saveFiles(input.getItem().getFiles(), input.getItem().getId().toString(), FileBusinessType.Meeting.toString(),FileClass.File.toString());
        output.setItem(oldEntity);
        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 删除信息，删除会议和通知
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public DeleteMeetingOutput deleteMeeting(DeleteMeetingInput input) {
        DeleteMeetingOutput output = new DeleteMeetingOutput();
        //查询会议主表并更新字段；
        Meeting oldEntity = meetingMapper.selectByPrimaryKey(input.getMeetingId());
        oldEntity.setDeleted(true);
        meetingMapper.updateByPrimaryKey(oldEntity);
        //查询通知主表并更新字段；
        Example epMeetingNotice = new Example(MeetingNotice.class);
        Example.Criteria ecMeetingNotice = epMeetingNotice.createCriteria();
        ecMeetingNotice.andEqualTo("meetingId", oldEntity.getId());
        List<MeetingNotice> meetingNoticeLs = meetingNoticeMapper.selectByExample(epMeetingNotice);
        meetingNoticeLs.forEach((k) -> {
            k.setDeleted(true);
            meetingNoticeMapper.updateByPrimaryKey(k);
        });
        output.setItem(oldEntity);
        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 根据会议ID获取会议信息（我发出的）
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public GetMeetingOutput getMeetingsByMe(GetMeetingInput input) {
        GetMeetingOutput output = new GetMeetingOutput();
        input.setUserCode(CurrentUser.getUserCode());
        //获取会议信息
        Meeting m = meetingMapper.selectByPrimaryKey(input.getMeetingId());

        //获取通知信息
        Example epMeetingNotice = new Example(MeetingNotice.class);
        Example.Criteria ecMeetingNotice = epMeetingNotice.createCriteria();
        ecMeetingNotice.andEqualTo("meetingId", m.getId());
        ecMeetingNotice.andEqualTo("deleted","0");
        List<MeetingNotice> ls = meetingNoticeMapper.selectByExample(epMeetingNotice);
        m.setMeetingNoticeList(ls);
        m.setFiles(fileService.getFiles(m.getId().toString(),FileBusinessType.Meeting.toString(),FileClass.File.toString()));
        output.setMeeting(m);
        return output;

    }

    /**
     *  创建人:王增立
     *  备注: 根据会议ID获取会议信息（发给我的）
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public GetMeetingOutput getMeetingsToMe(GetMeetingInput input) {
        GetMeetingOutput output = new GetMeetingOutput();
        input.setUserCode(CurrentUser.getUserCode());
        //获取会议信息
        Meeting m = meetingMapper.selectByPrimaryKey(input.getMeetingId());

        //获取通知信息
        Example epMeetingNotice = new Example(MeetingNotice.class);
        Example.Criteria ecMeetingNotice = epMeetingNotice.createCriteria();
        ecMeetingNotice.andEqualTo("meetingId", m.getId());
        ecMeetingNotice.andEqualTo("deleted","0");
        //如果相等说明是发起人，通知取所有，如果不是发起人，说明是通知人取自己的信息；
        //if (!input.getUserCode().equals(m.getCreatorCode())) {
        //ecMeetingNotice.andEqualTo("userCode", input.getUserCode());
         // }
        List<MeetingNotice> ls = meetingNoticeMapper.selectByExample(epMeetingNotice);
        m.setMeetingNoticeList(ls);
        m.setFiles(fileService.getFiles(m.getId().toString(),FileBusinessType.Meeting.toString(),FileClass.File.toString()));
        output.setMeeting(m);
        return output;

    }



    /**
     *  创建人:王增立
     *  备注: 根据發給我的会议通知status
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public PageLoadMeetingListOutput pageLoadMeetingListToMe(PageLoadMeetingInput input) {
        PageLoadMeetingListOutput output = new PageLoadMeetingListOutput();
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "gmt_status,gmt_create desc");
        input.setCurrentUserCode(CurrentUser.getUserCode());
        List<Meeting> ls=meetingMapper.getMeetingByNotesUserCode(input);
        PageInfo<Meeting> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Meeting>(page.getList(), (int) page.getTotal()));
        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 我发出的会议
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public PageLoadMeetingListOutput pageLoadMeetingListByMe(PageLoadMeetingInput input) {
        PageLoadMeetingListOutput output = new PageLoadMeetingListOutput();
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "Id desc");
        //获取通知信息
        input.setCurrentUserCode(CurrentUser.getUserCode());
        List<Meeting> ls = meetingMapper.getMeetingByCurrentUserCode(input);

        PageInfo<Meeting> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Meeting>(page.getList(), (int) page.getTotal()));
        return output;

    }



    /**
     *  创建人:王增立
     *  备注: 根据时间范围和人查询会议信息(需要关联查询）
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    @Transactional
    public List<GetMettingByTimeUserOutput> getMettingByTimeUser(GetMettingByTimeUserInput input) {
       // List<GetMettingByTimeUserOutput> outputList=new ArrayList<GetMettingByTimeUserOutput>();
        //获取通知信息
        input.setUserCode(CurrentUser.getUserCode());
        List<GetMettingByTimeUserOutput> ls = meetingMapper.getMeetingByCurrentUserTime(input);
        return ls;
    }

    /**
     *  创建人:王增立
     *  备注: 回复会议通知
     *  时间: 2019/4/26 15:09
     *  参数: 
     */
    public ReplyMeetingNoticeOutput replyMettingNotice(ReplyMeetingNoticeInput input) {
        ReplyMeetingNoticeOutput output = new ReplyMeetingNoticeOutput();

        //获取会议回复信息
        Example ep = new Example(MeetingNotice.class);
        Example.Criteria ec = ep.createCriteria();
        MeetingNotice m = meetingNoticeMapper.selectByPrimaryKey(input.getId());
        m.setReplyContent(input.getReplyContent());
        m.setReplyCategory(input.getReplyCategory());
        m.setReplyTime(new Date());
        meetingNoticeMapper.updateByPrimaryKey(m);
        output.setItem(m);

        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 确认会议通知
     *  时间: 2019/4/26 15:10
     *  参数: 
     */
    public ConfirmReplyCategoryOutPut confirmReplyMettingNotice(ConfirmReplyCategoryInput input) {
        ConfirmReplyCategoryOutPut output = new ConfirmReplyCategoryOutPut();

        //获取会议回复信息
        Example ep = new Example(MeetingNotice.class);
        Example.Criteria ec = ep.createCriteria();
        input.getConfirmReplyListMeetingNotice().forEach(k->{
            MeetingNotice m = meetingNoticeMapper.selectByPrimaryKey(k.getId());
            m.setConfirmReplyContent(k.getConfirmReplyContent());
            m.setConfirmReplyCategory(k.getConfirmReplyCategory());
            m.setConfirmReplyTime(new Date());
            meetingNoticeMapper.updateByPrimaryKey(m);
        });

        output.setNum(input.getConfirmReplyListMeetingNotice().size());

        return output;
    }

}
